<?php
namespace Admin\Model;
use Think\Model;
class CatModel extends Model{
    protected $_validate=array(
        array('cat_name','require','栏目名不能为空'),
        array('cat_name','3,16','栏目名必须为3到16位',2,'length')
    );
    protected $_auto=array(
        array('intro','createSn',3,'callback')//回调方法，表示填充的内容是当前模型的一个方法
    );
    function createSn(){
        return 'EC'.date('Ymd').mt_rand(10000,99999);
    }
    /**
     * find sons
     * @param $data
     * @param int $id
     * @param int $lev
     * @return array
     */
    public function getCatTree($data,$id=0,$lev=0){
        static $tree=array();
        foreach($data as $k => $v) {
            if($v['parent_id']==$id) {
                $v['lev']=$lev;
                $tree[]=$v;
                $this->getCatTree($data,$v['cat_id'],$lev+1);
            }
        }
        return $tree;
    }
    /**
     * find family
     * @param $arr
     * @param $parent_id
     * @return mixed
     */
    public function getFamily($arr,$parent_id){
        $tree=array();
        while($parent_id > 0){
            foreach ($arr as $v){
                if ($v['cat_id'] == $parent_id){
                    $tree[]=$v;
                    $parent_id=$v['parent_id'];
                    break;
                }
            }
        }
        return array_reverse($tree);
    }
    /**
     * test user login
     * @return bool
     */
    public function acc(){
        if (!isset($_SESSION['user_naem']) || !isset($_SESSION['ccode'])){
            return false;
        }else{
            return $_SESSION['ccode'] === md5(SALT.'|||'.$_SESSION['user_name']);
        }
    }
}